package com.scoremg.api;

import com.scoremg.annotation.GlobalInterceptor;
import com.scoremg.entity.po.CourseInfo;

import com.scoremg.entity.po.UserInfo;
import com.scoremg.entity.query.CourseInfoQuery;


import com.scoremg.entity.query.LayuiSearchQuery;
import com.scoremg.entity.vo.LayuiResultVO;
import com.scoremg.entity.vo.PaginationResultVO;
import com.scoremg.entity.vo.ResponseVO;

import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

import com.scoremg.service.CourseInfoService;

@RestController
@RequestMapping("/course")
@Validated
public class CourseInfoController extends ABaseController {

	@Resource
	private CourseInfoService courseInfoService;


	/**
	 * 查询课程信息
	 * @param query 查询参数
 	 * @return
	 */
	@RequestMapping("/loadCourseList")
	@GlobalInterceptor
	public LayuiResultVO loadCourseList(@RequestBody LayuiSearchQuery<CourseInfoQuery> query) {
		CourseInfoQuery infoQuery = new CourseInfoQuery();
		infoQuery.setPageNo(query.getPage());
		infoQuery.setPageSize(query.getLimit());
		BeanUtils.copyProperties(query.getData(), infoQuery);
		PaginationResultVO<CourseInfo> listByPage = courseInfoService.findListByPage(infoQuery);
		LayuiResultVO<UserInfo> layuiResultVO = getSuccessLayuiResultVO(listByPage.getList());
		layuiResultVO.setCount(listByPage.getTotalCount());
		return layuiResultVO;
	}

	/**
	 * 保存课程信息
	 * @param courseInfo 课程信息实体
	 * @return
	 */
	@RequestMapping("/saveCourseInfo")
	@GlobalInterceptor(checkAdmin = true)
	public ResponseVO saveCourse(@Validated CourseInfo courseInfo) {
		courseInfoService.saveCourse(courseInfo);
		return getSuccessResponseVO(null);
	}

	/**
	 * 删除课程
	 * @param courseId 课程id
	 * @return
	 */
	@RequestMapping("/delCourse")
	@GlobalInterceptor(checkAdmin = true)
	public ResponseVO delCourse(@NotNull Integer courseId) {
		courseInfoService.deleteCourseInfoByCourseId(courseId);
		return getSuccessResponseVO(null);
	}



}